Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

231
Views
FeathersJS Error Handler Unexpected Token < Issue

I am working on an Express App with MongoDB and trying to utilize FeathersJS for all my services. Here I'm running a test try to get an error message from the server to the client, but I have an issue with the response from the error handler. My req headers have the correct application/json stuff, so I assumed the Error Handler should send valid json back.

I know I'm not using the next callback in my function, but when I try to do that it gives the same error, so I'm thinking it has to do with the Error Handler. Any direction here would be greatly appreciated!

The first error log is on the server, which is correct.

Bucket Services
error >>>>> Bucket validation failed
Possibly Unhandled Rejection: Bucket validation failed,  Promise { <rejected> 'Bucket validation failed' }
>>>>>> Error: Unexpected token < in JSON at position 0
    at convert (/Users/jaruesink/Documents/Projects/Buckets/node_modules/feathers-rest/node_modules/feathers-errors/lib/index.js:365:79)
    at toError (/Users/jaruesink/Documents/Projects/Buckets/node_modules/feathers-rest/lib/client/base.js:24:37)
    at process._tickCallback (internal/process/next_tick.js:103:7)

my create function within the BucketService class:

  create({
    amount,
    isFund = false,
    name,
    type,
    userID: owner
  }, params, next) {
    const new_bucket = new Bucket({ name, amount, type, isFund, owner });
    return new_bucket.save((error) => {
      console.log('error >>>>>', error.message);
      if (error) { return Promise.reject(error.message); }
      return Promise.resolve(new_bucket);
    });
  }

my router file:

const feathers = require('feathers');
const errorHandler = require('feathers-errors/handler');
const rest = require('feathers-rest');
const router = feathers();

const LoginService = require('../services/login_service');
const UserService = require('../services/user_service');
const BucketService = require('../services/bucket_service');

// Enable REST services
router.configure(rest());

router.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  next();
});

router.use('/login', new LoginService());
router.use('/user', new UserService());
router.use('/bucket', new BucketService());

// Set up error handling
router.use(errorHandler());

module.exports = router;
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I figured it out, the key was to correctly pass through a callback (next) function as the third parameter to handle errors. FeathersJS handles the Promise Rejections for you on errors. Then in my test I needed to convert the Feathers-Error to JSON before I could get the message.

I changed my test to:

  it('can validate an incorrect bucket', (done) => {
    const invalid_bucket = {
      name: 'Invalid Bucket',
    };
    bucket_service.create(invalid_bucket, {}, (error) => {
      error = error.toJSON();
      assert(error.message.length > 0);
      done();
    });
  });

and my create function to:

  create({
    amount,
    isFund = false,
    name,
    type,
    userID: owner
  }, params, next) {
    const new_bucket = new Bucket({ name, amount, type, isFund, owner });
    return new_bucket.save()
      .then(created_bucket => Promise.resolve(created_bucket))
      .catch(next);
  }
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!